package com.jdf.org.service.impl;

import com.jdf.basic.util.PageList;
import com.jdf.org.domain.Department;
import com.jdf.org.mapper.DepartmentMapper;
import com.jdf.org.query.DepartmentQuery;
import com.jdf.org.service.IDepartmentService;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class IDepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);
    }

    @Override
    @Transactional
    public void del(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.load(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    /*@Override
    public Integer queryCount(DepartmentQuery query) {
        return departmentMapper.queryCount(query);
    }*/

    @Override
    public PageList<Department> queryData(DepartmentQuery query) {
        Long count = departmentMapper.queryCount(query);
        if (count<1){
            return new PageList<Department>();
        }
        List<Department> Dlist = departmentMapper.queryData(query);
        return new PageList<Department>(count,Dlist);

    }

    @Override
    public void pathDelte(List<Long> ids) {
        departmentMapper.pathDelte(ids);
    }

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.loadAll();
        Map<Long,Department> map  = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        List<Department> deptTree = new ArrayList<>();
        for (Department department:departments){
            if(department.getParent_id() == null){//顶级部门
                deptTree.add(department);
            }else{//不是顶级部门
                //1.想办法找到当前部门的 父部门
                Long parent_id = department.getParent_id();
                //这种从数据库查询的方式，每次通过id去查询 - 查询出来映射给实体domain，children集合中都会是空集合 - 没有任何数据
                //Department parentDepartment = departmentMapper.loadById(parent_id);
                //下面这种是从Map中去获取的 - 以前装进去的对象还是会存在
                Department parentDepartment = map.get(parent_id);
                //2.将自己装到  父部门 的children中
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
